package DBConnection;


import Services.Temperatura;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

/**
 *
 * @author Sérgio Câmara
 */
public class TemperaturaDB {
    //public static Temperatura tempDB;
    private String stmt = new String();
    
    public void gravaTemperatura(Temperatura temp) {
       
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");       
        
        MySqlDB banco = new MySqlDB("jdbc:mysql://localhost/coletortemperatura?user=root&password=root");
        banco.connect();  //conecta ao banco
        MySqlQuery qry = new MySqlQuery(banco.conn);
        
        stmt = "insert into temperaturas(temp_data, temp_valor) values ('" +                 
                dateFormat.format(temp.getDataTemperatura()) + "'," +
                temp.getValorTemperatura() + ")";       
        
        qry.execute(stmt);        
        
        banco.disconnect();
    }
    
    public Temperatura consultaTemperatura(Temperatura temp) {
        
        MySqlDB banco = new MySqlDB("jdbc:mysql://localhost/coletortemperatura?user=root&password=root");
        banco.connect();  //conecta ao banco
        MySqlQuery qry = new MySqlQuery(banco.conn);
        
        stmt = "select * from temperaturas order by temp_data desc limit 1";
        
        qry.open(stmt);
        
        qry.last();
            do {
                //temp.setValorTemperatura(qry.fieldbyname("temp_valor"));
                //temp.setValorData(qry.fieldbyname("temp_data"));               
            } while (qry.prev());
        
        return temp;
    }
    
    
}
